package cn.juque.common.disruptor;

import cn.hutool.extra.spring.SpringUtil;
import cn.juque.common.base.DisruptorBody;
import cn.juque.common.base.DisruptorMessage;
import com.lmax.disruptor.EventHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author nuoka
 * @version 1.0.0
 * <li>IntelliJ IDEA</li>
 * <li></li>
 * @date 2021/8/24 22:56
 **/
@Slf4j
public class MqEventHandler implements EventHandler<DisruptorMessage<? extends DisruptorBody>> {

    @Override
    public void onEvent(DisruptorMessage messageBO, long l, boolean b) {
        DisruptorBody disruptorBodyBO = messageBO.getBody();
        if(null == disruptorBodyBO) {
            log.warn("disruptor found body null");
            return;
        }
        IBusinessEventService businessEventService = SpringUtil.getBean(disruptorBodyBO.getBusinessEventService());
        if(null == businessEventService) {
            log.error("event name:{}, bean is null", disruptorBodyBO.getBusinessEventService());
            return;
        }
        try {
            businessEventService.invoke(disruptorBodyBO);
        } catch (Exception e) {
            log.error("disruptor invoke error", e);
        }
    }
}
